package com.qb.login.ui.activity

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.CompoundButton
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SPStaticUtils
import com.blankj.utilcode.util.ToastUtils
import com.qb.common.base.CommonBaseActivity
import com.qb.common.constant.Constants
import com.qb.common.router.RouterPath
import com.qb.common.utils.LitePalUtils
import com.qb.common.utils.ValidUtils
import com.qb.login.databinding.LoginActivityLoginBinding
import com.qb.login.ui.viewmodel.LoginViewModel

@Route(path = RouterPath.Login.URL_Login_LoginActivity)
class LoginActivity : CommonBaseActivity<LoginViewModel, LoginActivityLoginBinding>(),View.OnClickListener {
    var registerId:String?=null
//    override fun layoutId(): Int = R.layout.login_activity_login
    var isCheck = false
    override fun initView(savedInstanceState: Bundle?) {
        mBinding.btnLogin.setOnClickListener(this)
        mBinding.tvRegister.setOnClickListener(this)
        mBinding.privacyAgreementTv.setOnClickListener(this)
        mBinding.llUserXy.setOnClickListener(this)
        isCheck = SPStaticUtils.getBoolean(Constants.USER_AGREEMENT)
        mBinding.agreeCb.isChecked = isCheck
        mBinding.agreeCb.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { buttonView: CompoundButton?, isChecked: Boolean ->
            isCheck = isChecked
        })
    }

    private fun login() {

    }

    override fun initData() {
        registerId = intent.getStringExtra("registerId")
    }

    override fun onClick(view: View?) {
        view?.let {
            when(view.id){
                mBinding.btnLogin.id->{
                    if (TextUtils.isEmpty(mBinding.etAccount.text)){
                        ToastUtils.showShort("账户不能为空")
                        return
                    }
                    if (TextUtils.isEmpty(mBinding.etPassword.text)){
                        ToastUtils.showShort("密码不能为空")
                        return
                    }
                    if (!isCheck){
                        ToastUtils.showShort("请仔细阅读后勾选同意隐私协议")
                        return
                    }
//                    try {
                        var userInfo = LitePalUtils.findUser("${mBinding.etAccount.text}")
                        if (ValidUtils.isValid(userInfo)){
                            SPStaticUtils.put(Constants.LOGININFO,GsonUtils.toJson(userInfo))
    //                        ARouter.getInstance().build(RouterPath.App.URL_App_MainActivity).navigation()
                            finish()
                        } else {
                            ToastUtils.showShort("该账号未注册")
                        }
//                    } catch (e: Exception) {
//                        ToastUtils.showShort("该账号未注册")
//                    }
                }
                mBinding.privacyAgreementTv.id,mBinding.llUserXy.id->{
                    val packageName = AppUtils.getAppPackageName()
                    val className = "com.qb.common.ui.activity.WebViewActivity"
                    // 创建一个 Intent
                    val intent = Intent().apply {
                        setClassName(packageName, className)
                    }
                    // 启动目标 Activity
                    startActivity(intent)
                }
                mBinding.tvRegister.id->{
                    ARouter.getInstance().build(RouterPath.Login.URL_Login_RegisterActivity).navigation()
                    finish()
                }

                else -> {}
            }
        }
    }
}